﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Innova.Entites;
using System.Data;


namespace Innova.Repository
{
    public class NewsRepository : MasterRepository, INewsRepository
    {
        public List<NEWS> GetAllFromNews()
        {
            var query = from c in Context.NEWSs.Include("PATROCINADOR")
                         select c;
            return query.ToList();
            
        }

        public List<NEWS> GetFromNewsByDescripcion(string descripcion)
        {
            if (descripcion != "")
            {
                var query = from c in Context.NEWSs.Include("PATROCINADOR")
                            where c.Titulo.ToUpper().Contains(descripcion.ToUpper()) ||
                            c.Comentario.ToUpper().Contains(descripcion.ToUpper())
                            select c;
                return query.ToList();
            }
            else
            {
                var query1 = from c in Context.NEWSs.Include("PATROCINADOR")
                             select c;
                return query1.ToList();
            }
        }

        public List<NEWS> GetFromNewsByPatrocinadorId(int PatrocinadorId)
        {
            var query = from c in Context.NEWSs.Include("PATROCINADOR")
                        where c.PatrocinadorId.Equals(PatrocinadorId)
                        select c;
            return query.ToList();
        }

        public NEWS GetFromNewsById(int id)
        {
            var query = from c in Context.NEWSs
                        where c.Id.Equals(id)
                        select c;
            return query.SingleOrDefault();
        }

        public void AddNews(NEWS news)
        {
            Context.NEWSs.Add(news);
            Context.SaveChanges();
        }

        public void UpdateNews(NEWS news)
        {
            Context.NEWSs.Attach(news);
            Context.Entry(news).State = EntityState.Modified;
            Context.SaveChanges();
        }

        public void RemoveNews(int id)
        {
            throw new NotImplementedException();
        }


        public byte[] GetFotoByNewsId(int id)
        {
            var query = from c in Context.NEWSs
                        where c.Id == id
                        select c.Imagen;

            return query.SingleOrDefault();
        }
    }
}
